package mobidever.mygodutch.bussiness;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import mobidever.mygodutch.bussiness.base.BussinessBase;
import mobidever.mygodutch.database.sqlitedal.SQLiteDALUser;
import mobidever.mygodutch.model.ModelUser;

public class BussinessUser extends BussinessBase
{
	private SQLiteDALUser mSqLiteDALUser;

	public BussinessUser(Context pContext)
	{
		super(pContext);
		mSqLiteDALUser = new SQLiteDALUser(pContext);
	}
	
	public boolean insertModelUser(ModelUser pModelUser)
	{
		return mSqLiteDALUser.insertUser(pModelUser);
	}
	
	public boolean deleteModelUserByID(int pID)
	{
		String _Condition = "and UserID = " + pID;
		return mSqLiteDALUser.deleteUser(_Condition);
	}
	
	public boolean updateModelUserByID(ModelUser pModelUser)
	{
		//int _ID = (int)pModelUser.getUserID();
		//String _Condition = "and UserID = " + _ID;
		return mSqLiteDALUser.updateUser(pModelUser);
	}
	
	public List<ModelUser> getNoNideUsers()
	{
		return getUsers("and State = 1"); 
	}
	
	public List<ModelUser> getUsers(String pCondtition)
	{
		return mSqLiteDALUser.getUsers(pCondtition);
	}

	public ModelUser getModelUserByUserID(int pUserID)
	{
		List<ModelUser> _List = getUsers("and UserID=" + pUserID) ;
		if (_List.size() == 1)
		{
			return _List.get(0);
		}else {
			return null;
		}
	}
	
	public String getUsernNameByUserID(String pUserID)
	{
		List<ModelUser> _List = getUserListByUserID(pUserID.split(","));
		String _Name = "";
		
		for (int i = 0; i < _List.size(); i++)
		{
			_Name += _List.get(i).getUserName() + ",";
		}
		
		return _Name;
	}
	
	public List<ModelUser> getUserListByUserID(String pUserID[])
	{
		List<ModelUser> _List = new ArrayList<ModelUser>();
		for (int i = 0; i < pUserID.length; i++)
		{
			_List.add(getUsers("and UserID = " + pUserID[i]).get(0));
		}
		return _List;
	}
	
	public boolean IsExistUserByUserName(String pUserName,Integer pUserID)
	{
		String _Condition = "and UserName='" + pUserName + "'" ;
		if (pUserID != null)
		{
			_Condition += "and UserID=" + pUserID;
		}
		List<ModelUser> _List = getUsers(_Condition);
		if (_List.size() == 1)
		{
			return true;
		}else {
			return false;
		}
	}
	
	public boolean HideUserByUserID(int pUserID)
	{
		String _Condition = "and UserID = " + pUserID;
		List<ModelUser> _List = getUsers(_Condition);
		if (_List.size() == 1)
		{
			ModelUser _ModelUser = _List.get(0);
			_ModelUser.setState(0);
			mSqLiteDALUser.updateUser(_ModelUser);
			return true;
		}
		
		return false;
	}
}
